package com.example.mentalarithmetic;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.List;

public class ChooseageActivity extends BaseActivity{
    private Book[] books={
            new Book("一年级上",R.mipmap.bookssjbshuxuexs1scover),
            new Book("一年级下",R.mipmap.bookssjbshuxuexs1xcover),
            new Book("二年级上",R.mipmap.bookssjbshuxuexs2scover),
            new Book("二年级下",R.mipmap.bookssjbshuxuexs2xcover),
            new Book("三年级上",R.mipmap.bookssjbshuxuexs3scover),
            new Book("三年级下",R.mipmap.bookssjbshuxuexs3xcover),
            new Book("四年级上",R.mipmap.bookssjbshuxuexs4scover),
            new Book("四年级下",R.mipmap.bookssjbshuxuexs4xcover),
            new Book("五年级上",R.mipmap.bookssjbshuxuexs5scover),
            new Book("五年级下",R.mipmap.bookssjbshuxuexs5xcover),
            new Book("六年级上",R.mipmap.bookssjbshuxuexs6scover),
            new Book("六年级下",R.mipmap.bookssjbshuxuexs6xcover)};
    private BookAdapter adapter;
    private List<Book> bookList=new ArrayList<>();
    private DrawerLayout mDrawerLayout;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chooseage);

        Toolbar toolbar=findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);
        mDrawerLayout=findViewById(R.id.drawer_layout);
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_list);
        }

        FloatingActionButton fab=findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view,"添加成功",Snackbar.LENGTH_SHORT)
                        .setAction("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(ChooseageActivity.this,"已取消",Toast.LENGTH_SHORT).show();
                            }
                        }).show();
            }
        });

        initBook();
        RecyclerView recyclerView=findViewById(R.id.recycler_view);
        GridLayoutManager layoutManager=new GridLayoutManager(this,2);
        recyclerView.setLayoutManager(layoutManager);
        adapter=new BookAdapter(bookList);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
                default:
        }
        return true;
    }

    private void initBook(){
        bookList.clear();
        for(Book e:books){
            bookList.add(e);
        }
    }
}
class BookAdapter extends RecyclerView.Adapter<BookAdapter.ViewHolder> {

    private Context mContext;
    private List<Book> mBookList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView bookImage;
        TextView bookName;
        CardView bookView;
        public ViewHolder(View view){
            super(view);
            bookView=(CardView) view;
            bookImage=view.findViewById(R.id.book_image);
            bookName=view.findViewById(R.id.book_name);
        }
    }
    public BookAdapter(List<Book> bookList){
        mBookList=bookList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(mContext==null){
            mContext=parent.getContext();
        }
        View view= LayoutInflater.from(mContext).inflate(R.layout.book_item,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.bookView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=holder.getAdapterPosition();
                Book book=mBookList.get(position);
                ComputeActivity.actionStart(holder.bookView.getContext(),book.getName(),book.getImageId());
            }
        });
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Book book=mBookList.get(position);
        holder.bookName.setText(book.getName());
        Glide.with(mContext).load(book.getImageId()).into(holder.bookImage);
    }

    @Override
    public int getItemCount() {
        return mBookList.size();
    }
}

class Book {
    private String name;
    private int imageId;
    public Book(String name,int imageId){
        this.name=name;
        this.imageId=imageId;
    }
    public String getName(){
        return name;
    }
    public int getImageId(){
        return imageId;
    }
}
